package com.ruoyi.common.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySmsTemplateListRequest;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySmsTemplateListResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.ruoyi.common.config.AliYunSmsProperties;
import com.ruoyi.common.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;

@Component
@EnableConfigurationProperties(AliYunSmsProperties.class)
public class AliYunSmsUtils {

    private static final String product = "Dysmsapi";// 短信API产品名称（短信产品名固定，无需修改）
    private static final String domain = "dysmsapi.aliyuncs.com";// 短信API产品域名（接口地址固定，无需修改）
    private static final Logger log = LoggerFactory.getLogger(AliYunSmsProperties.class);
    private DefaultProfile profile;

    @Resource
    private AliYunSmsProperties prop;

    public void send(HashMap<String, String> paramMap) {
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            profile = DefaultProfile.getProfile("cn-hangzhou", prop.getAccessKeyId(), prop.getAccessKeySecret());
        } catch (ClientException e) {
            log.error("ErrCode:" + e.getErrCode());
            return;
        }
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(paramMap.get("phone"));
        request.setSignName(paramMap.get("signName"));
        request.setTemplateCode(paramMap.get("code"));
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            log.info(new Gson().toJson(response));
        } catch (ClientException e) {
            log.error("ErrCode:" + e.getErrCode());
            log.error("ErrMsg:" + e.getErrMsg());
            log.error("RequestId:" + e.getRequestId());
        }
    }

    public QuerySmsTemplateListResponse querySmsTemplateList(int pageIndex, int pageSize) {
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            profile = DefaultProfile.getProfile("cn-hangzhou", prop.getAccessKeyId(), prop.getAccessKeySecret());
        } catch (ClientException e) {
            log.error("ErrCode:" + e.getErrCode());
            return null;
        }
        IAcsClient client = new DefaultAcsClient(profile);
        QuerySmsTemplateListRequest request = new QuerySmsTemplateListRequest();
        request.setPageIndex(pageIndex);
        request.setPageSize(pageSize>50?50:pageSize);
        try {
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            throw new BusinessException(e.getErrMsg());
        }
    }
}

